package com.broada.apm.service.internal.impl;

import java.util.Arrays;

import org.apache.wink.client.ClientResponse;

import com.broada.apm.entity.NoticeCommandException;
import com.broada.apm.service.internal.IndicatorService;
import com.broada.apm.utils.ConfigureProperties;
import com.broada.apm.utils.RestClientUtil;

/**
 * 先简单实现 写死url
 * 
 * @author Administrator Create By 2015年3月27日 下午4:51:17
 */
public class IndicatorServiceImpl implements IndicatorService {
	private String ip = ConfigureProperties.getString("hub.receive.notice.host", "127.0.0.1");
	private String port = ConfigureProperties.getString("hub.receive.notice.port", "7777");
	private String URL = "http://" + ip + ":" + port;

	@Override
	public void NoticeCommand(String act, String cat) throws NoticeCommandException {
		String url = URL + "/config/?act=" + act + "&cat=" + cat;
		ClientResponse response = RestClientUtil.doAction("get", url, null);
		String entity = response.getEntity(String.class);
		if (response.getStatusCode() > 300)
			throw new NoticeCommandException("指标运算通知失败:"+entity);
	}

	@Override
	public void NoticeCommand(String act, String cat, String... ids) throws NoticeCommandException {
		String url = URL + "/config/?act=" + act + "&cat=" + cat + "&id="
				+ Arrays.toString(ids).replaceAll("[\\[\\]\\s]", "");
		ClientResponse response = RestClientUtil.doAction("get", url, null);
		String entity = response.getEntity(String.class);
		if (response.getStatusCode() > 300)
			throw new NoticeCommandException("指标运算通知失败:"+entity);
	}

	@Override
	public void NoticeAlertRule(String act, String cat, String appId, String targetId, String thresholdType, String panelId) throws NoticeCommandException {
		String url = URL + "/config/?act=" + act + "&cat=" + cat + "&appId=" + appId + "&targetId=" + targetId + "&thresholdType=" + thresholdType + "&panelId=" + panelId;
		ClientResponse response = RestClientUtil.doAction("get", url, null);
		String entity = response.getEntity(String.class);
		if (response.getStatusCode() > 300)
			throw new NoticeCommandException("指标运算通知失败:"+entity);
	}

}
